package com.epoint.mobileoa.exception;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.HashMap;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.ConfigKey;
import com.epoint.mobileoa.bizlogic.FrameMethod;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.HttpUtil;
import com.epoint.mobileoa.tools.LogHelp;
import com.epoint.mobileoa.tools.PhoneHelp;

import android.content.Context;
import android.util.Log;

/**
 * @author liyc
 * @time 2012-12-28 下午5:10:38
 * @annotation 崩溃程序处理
 */
public class CrashHandler implements UncaughtExceptionHandler {

	private static CrashHandler INSTANCE = new CrashHandler();
	private Context mContext;

	private CrashHandler() {
	}

	public static CrashHandler getInstance() {
		return INSTANCE;
	}

	public void init(Context ctx) {
		mContext = ctx;
		Thread.setDefaultUncaughtExceptionHandler(this);
	}

	@Override
	public void uncaughtException(Thread thread, final Throwable ex) {
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				uploadErrorInfo(ex);
			}
		}).start();
		
		/*
		new Thread() {
			@Override
			public void run() {
				if(mContext.getClass().getSuperclass().getName().equals("android.app.Service"))
				{
					uploadErrorInfo(ex);
				}
				else{
					Looper.prepare();
					new AlertDialog.Builder(mContext)
					.setTitle(R.string.prjname)
					.setIcon(R.drawable.epoint2).setCancelable(false)
					.setMessage("程序错误，请您退出软件重新打开！\n\n错误原因：\n"+ex.getLocalizedMessage()).setNeutralButton("退出程序", new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							uploadErrorInfo(ex);
						}
					})
					.create().show();
					Looper.loop();
				}
			}
		}.start();
	*/}

	public void uploadErrorInfo(Throwable ex)
	{
		String userguid = DBHelp.getConfigValue(mContext, ConfigKey.userguid);
		String displayname = DBHelp.getConfigValue(mContext, ConfigKey.displayname);
		String SystemSDK = android.os.Build.VERSION.SDK_INT+"";
		String MANUFACTURER = android.os.Build.MANUFACTURER+"-"+android.os.Build.MODEL;
		String deviceid = PhoneHelp.getDeviceId(mContext);
		StringBuilder sb = new StringBuilder();
		sb.append("<message>"+ex.getMessage()+"</message><class>"+mContext.getClass().getName()+"</class><stack>");
		int len = ex.getStackTrace().length;
		Log.e("EPOINT-ERROR", ex.getMessage());
		for(int i=0;i<len;i++)
		{
			sb.append(ex.getStackTrace()[i]);
			Log.e("EPOINT-ERROR", ex.getStackTrace()[i].toString());
		}
		sb.append("</stack>");
		String seamurl = DBHelp.getConfigValue(mContext, ConfigKey.seamurl);
		if(seamurl.equals(""))
		{
			seamurl = FrameMethod.newIpUrl(mContext, mContext.getString(R.string.ip));
		}
		seamurl+= "?requestType=uploadErrorLog";
		//    	System.out.println(
		//    			"url:"+seamurl+
		//    			"\nuserguid:"+userguid+
		//    			"\ndisplayname:"+displayname+"\n"+
		//    			"\nSystemSDK:"+SystemSDK+"\n"+
		//    			"\nMANUFACTURER:"+MANUFACTURER+"\n"+
		//    			"\ndeviceid:"+deviceid+"\n"
		//    			);
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("userguid", userguid);
		params.put("displayname", displayname);
		params.put("SystemSDK", SystemSDK);
		params.put("MANUFACTURER", MANUFACTURER);
		params.put("deviceid", deviceid);
		params.put("errorlog", sb.toString());
		params.put("releaseVersion", "Android_V"+mContext.getString(R.string.version));
		try{
			//保存到SD卡
			LogHelp.Log2SDInfo("<userguid>"+userguid+"</userguid>"+
					"<displayname>"+displayname+"</displayname>"+
					"<SystemSDK>"+SystemSDK+"</SystemSDK>"+
					"<MANUFACTURER>"+MANUFACTURER+"</MANUFACTURER>"+
					"<deviceid>"+deviceid+"</deviceid>"+
					"<errorlog>"+sb.toString()+"</errorlog>"
			);
			//上传到后台
			HttpUtil.post(seamurl, params, null);
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		MainService.quitApplication(mContext);

	}
}